/**
 * @file modbus_task.h
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2025-01-08
 *
 * @copyright Copyright (c) 2025
 *
 */

#ifndef __MODBUS_TASK_H
#define __MODBUS_TASK_H

/* includes ----------------------------------------------------------*/
#include "FreeRTOS.h"
#include "port.h"
#include "task.h"
#include <string.h>

/* typedef -----------------------------------------------------------*/
typedef enum
{
    eIdle  = 0,
    eWrite = 1,
    eRead  = 2,
    eWarn  = 3,
    eOther = 4,
} MotorState_t;

#pragma pack(1)

typedef struct
{
    uint8_t      IsWarn;
    MotorState_t state;
} state_contrl_t;

typedef struct
{
    uint8_t request_id;
    uint8_t registers;
    uint8_t functions;
    uint8_t NRegs;
} MotorCtrlPara_t;

typedef struct
{
    int32_t ilWrite;
    USHORT  usModbusUserData[2];   // 在这里使用出现问题
    UCHAR   ucModbusUserData[2];
} register_buffer_t;

#pragma pack()

/* define ------------------------------------------------------------*/
// Modbus主机任务优先级
#define RTU_MASTER_TASK_PRIO (configMAX_PRIORITIES - 2)
// Modbus主机任务堆栈大小
#define RTU_MASTER_STK_SIZE 128 * 4

// Modbus从机任务优先级
#define RTU_SLAVE_TASK_PRIO (configMAX_PRIORITIES - 2)
// Modbus从机任务堆栈大小
#define RTU_SLAVE_STK_SIZE 128 * 4

// 任务优先级
#define RTU_MASTER_APP_TASK_PRIO (configMAX_PRIORITIES - 5)
// 任务堆栈大小
#define RTU_MASTER_APP_STK_SIZE 128 * 4

#define mtControlStateReg (0x0006)
#define mtReadWarnState (0x00A3)
#define mtSetAbsolutePosReg (0x00E8)

/* variables ---------------------------------------------------------*/
extern TaskHandle_t RtuMasterTask_Handler;
extern TaskHandle_t RtuSlaveTask_Handler;
extern TaskHandle_t RtuMasterAppTask_Handler;
/* function prototypes -----------------------------------------------*/
/**
 * @brief
 *
 * @param pvParameters
 */
void rtu_master_task(void* pvParameters);

/**
 * @brief
 *
 * @param pvParameters
 */
void rtu_master_app_task(void* pvParameters);
/**
 * @brief Modbus从机轮训线程
 *
 * @param pvParameters
 */
void rtu_slave_task(void* pvParameters);


#endif
